<template>
  <div class="contact">
    <x-header :left-options="{showBack: false}">联系人</x-header>
    <my-tabbar></my-tabbar>
    <div class="contact-wrap">
      <div class="contact-row"
           v-for="(item, index) in $store.state.chat.contacts"
           :key="index"
           @click="toChat(item)">
        <div class="avatar-wrap">
          <badge v-if="item.noWriteNum" class="avatar-badge" :text="item.noWriteNum"></badge>
          <img slot="icon" class="avatar" src="../../assets/avatar.jpg">
        </div>
        <div class="title-wrap">
          <div class="title">{{item.UserCode}}</div>
          <div class="sub-title">{{item.companyName}}</div>
          <!-- <div class="sub-title">{{item.messages.length ? item.messages[item.messages.length - 1].message : ''}}</div> -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  XHeader,
  Group,
  Cell,
  Panel,
  Badge
} from 'vux'
import MyTabbar from '../../components/MyTabbar.vue'
import {
  getLink,
  disposeNoWriteMsg
} from '../../api/api'
export default {
  name: 'contact',
  components: {
    XHeader,
    Group,
    Cell,
    Panel,
    Badge,
    MyTabbar
  },
  data () {
    return {}
  },
  mounted () {
    this.getContacts()
  },
  methods: {
    toChat (contact) {
      this.$store.state.chat.contact = contact
      this.$router.push({
        path: '/main/chat'
      })
      if (contact.senderIdData.length) {
        this.disposeNoWriteMsg(contact)
      }
    },
    getContacts () {
      const user = this.$store.state.user
      getLink({}).then(res => {
        if (res.status === 200) {
          const data = res.data
          const contacts = []
          this.$store.state.chat.contactBadge = 0
          data.map(item => {
            if (item.senderId !== user.uid) {
              this.$store.state.chat.contactBadge += item.noWriteNum
              contacts.push(item)
            }
          })
          this.$store.state.chat.contacts = contacts
        } else {
          this.$vux.toast.text(res.msg)
        }
      })
    },
    disposeNoWriteMsg (contact) {
      disposeNoWriteMsg({
        friUserId: contact.senderId
      }).then(res => {
        if (res.status === 200) {
        } else {
          this.$vux.toast.text(res.msg)
        }
      })
    }
  }
}
</script>

<style lang="less">
.contact{
  /*.contact-wrap{*/
    /*margin: 10px 0;*/
  /*}*/
  .contact-row{
    display: flex;
    .avatar-wrap{
      padding: 10px;
      position: relative;
      .avatar-badge{
        position: absolute;
        right: 5px;
        top: 5px;
      }
      .avatar{
        width: 50px;
        height: 50px;
      }
    }
    .title-wrap{
      padding-right: 10px;
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      border-bottom: 1px solid #D9D9D9;

      .sub-title{
        height: 25px;
        font-size: 14px;
        color: grey;
      }
    }
  }
}
</style>
